import { createSlice } from '@reduxjs/toolkit'
import request from '../../utils/request'

const profile = createSlice({
  name: 'profile',
  initialState: {
    user: {},
    userProfile: {},
  },
  reducers: {
    saveUser: (state, action) => {
      state.user = action.payload
    },
    saveProfile: (state, action) => {
      state.userProfile = action.payload
    },
  },
})

export const { saveUser, saveProfile } = profile.actions

//
export const getUserAsync = () => {
  return async (dispatch) => {
    const res = await request({
      method: 'get',
      url: '/user',
    })
    dispatch(saveUser(res.data))
  }
}
//
export const getProfileAsync = () => {
  return async (dispatch) => {
    const res = await request({
      method: 'get',
      url: '/user/profile',
    })
    dispatch(saveProfile(res.data))
    // console.log(res)
  }
}
export default profile.reducer
